package com.china_goldcard.myguideview;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

import me.relex.circleindicator.CircleIndicator;

/**
 * 欢迎页视图
 */

public class MyGuideView extends RelativeLayout {

    public interface OnBtnClickedListener{
        void onClick(View view);
    }

    private ViewPager viewPager;

    private CircleIndicator circleIndicator;

    private boolean setBgColor = true;

    private Button btn;

    private List<GuideInfo> images;

    private String btnTitle = "立即体验";

    private MyGuideViewPageAdapter<ImageView> adapter;

    private Context context;

    private OnBtnClickedListener btnClickedListener;

    public MyGuideView(Context context){
       this(context,null);
    }

    public MyGuideView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        getConfig(context,attrs);
        initView(context);
    }

    /**
     * 从xml中获取配置信息
     */
    private void getConfig(Context context, AttributeSet attrs) {
        //TypedArray是一个数组容器用于存放属性值
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.MyGuideView);
        btnTitle = attributes.getString(R.styleable.MyGuideView_btnTitle);
        //用完务必回收容器
        attributes.recycle();
    }

    /**
     * 初始化视图
     * @param context
     */
    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.guide_view,this,true);
        viewPager = (ViewPager) findViewById(R.id.guide_view_pager);
        btn = (Button) findViewById(R.id.guide_view_btn);
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (btnClickedListener != null){
                    btnClickedListener.onClick(v);
                }
            }
        });
        adapter = new MyGuideViewPageAdapter<ImageView>();
        viewPager.setAdapter(adapter);
        circleIndicator = (CircleIndicator) findViewById(R.id.guide_circle_indicator);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == images.size() - 1){
                    btn.setVisibility(VISIBLE);
                }else{
                    btn.setVisibility(GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
      updateUI();
    }

    /**
     * 更新ui
     */
    private void updateUI(){
        this.btn.setText(btnTitle);
    }


    public void setBtnClickedListener(OnBtnClickedListener btnClickedListener) {
        this.btnClickedListener = btnClickedListener;
    }

    /**
     * 设置image
     * @param images
     */
    public void setImages(List<GuideInfo> images,boolean setBgColor) {
        this.images = images;
        this.setBgColor = setBgColor;
        List<ImageView> imageViews = new ArrayList<>();
        for (GuideInfo image : images){
            ImageView view = new ImageView(context);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
            view.setLayoutParams(params);
            view.setScaleType(ImageView.ScaleType.CENTER);
            view.setImageDrawable(image.getImage());
            if (setBgColor){
                view.setBackgroundColor(image.getBgColor());
            }
            imageViews.add(view);
        }
        adapter.reloadData(imageViews);
        circleIndicator.setViewPager(viewPager);
    }
}
